package com.kgc.ljp.dao.impl;

import com.kgc.ljp.dao.NewsDao;
import com.kgc.ljp.entity.News;
import com.kgc.ljp.util.BaseDao;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.xpath.internal.operations.And;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl extends BaseDao implements NewsDao {
    News news = null;
    static int count = 0;
    List<News>  newsList = null;
    @Override
    public List<News> getList() {
        conn = BaseDao.getConnection();
        newsList = new ArrayList<>();
        String sql = "SELECT `id`,`categoryId`,`title`,`summary`," +
                "`content`,`picPath`,`author`,`createDate`,`modifyDate`" +
                " FROM `news_detail`";
        try {
            rs = select(sql,null);
            while (rs.next()){
                news = new News(rs.getInt(1),rs.getInt(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getString(6),
                        rs.getString(7),rs.getDate(8),rs.getDate(9));
                newsList.add(news);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
        return newsList;
    }

    @Override
    public List<News> getListById(int id) {
        conn = BaseDao.getConnection();
        newsList = new ArrayList<>();
        String sql = "SELECT `id`,`categoryId`,`title`,`summary`," +
                                            "`content`,`picPath`,`author`,`createDate`,`modifyDate`" +
                                            " FROM `news_detail`" +
                                           "where id = ?";
        Object [] param = {id};
        try {
            rs = select(sql,param);
            while (rs.next()){
                news = new News(rs.getInt(1),rs.getInt(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getString(6),
                        rs.getString(7),rs.getDate(8),rs.getDate(9));
                newsList.add(news);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
        return newsList;
    }

    @Override
    public boolean updateNews(News news) {
        conn = BaseDao.getConnection();
        this.news = news;
        String sql = "UPDATE `news_detail` SET `categoryId`=?," +
                "`title`=?,`summary`=?,`content`=?,`picPath`=?,`author`=?," +
                "`createDate`=?,`modifyDate`=? where id = ?";
        Object [] param = {news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getPicPath()
        ,news.getAuthor(),news.getCreateDate(),news.getModifyDate(),news.getId()};
        try {
            if(update(sql,param)>0){
                return true;
            }else{
                return false;
            }

        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
    }

    @Override
    public boolean deleteNews(int id) {
        conn = BaseDao.getConnection();
        String sql = "DELETE FROM `news_detail` WHERE id = ?";
        Object [] param = {id};
        try {
            if(update(sql,param)>0){
                return true;
            }else{
                return false;
            }
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
    }

    @Override
    public Boolean addNews(News news) {
        this.news = news;
        conn = BaseDao.getConnection();
        this.news = news;
        String sql = "INSERT INTO `news_detail`(`categoryId`,`title`,`summary`,`content`,`picPath`,`author`,`createDate`,`modifyDate`) " +
                "VALUES (?,?,?,?,?,?,?,?)";
        Object [] param = {news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getPicPath()
                ,news.getAuthor(),news.getCreateDate(),news.getModifyDate()};
        try {
            if(update(sql,param)>0){
                return true;
            }else{
                return false;
            }
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
    }

    @Override
    public List<News> selectByInfo(int type, String title) {
        conn = BaseDao.getConnection();
        newsList = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT `id`,`categoryId`,`title`,`summary`," +
                "`content`,`picPath`,`author`,`createDate`,`modifyDate`" +
                " FROM `news_detail` where 1=1 ");
        if (!title.equals("0")){
            sql.append("AND title='"+title+"'");
        }
        if (type != 0){
            sql.append("AND categoryId="+type);
        }
        try {
            rs = select(sql.toString(),null);
            while (rs.next()){
                news = new News(rs.getInt(1),rs.getInt(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getString(6),
                        rs.getString(7),rs.getDate(8),rs.getDate(9));
                newsList.add(news);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
        return newsList;
    }

    @Override
    public List<News> getListByLimit(int pageNum,int curPage) {
        conn = BaseDao.getConnection();
        newsList = new ArrayList<>();
        String sql = "SELECT `id`,`categoryId`,`title`,`summary`," +
                "`content`,`picPath`,`author`,`createDate`,`modifyDate`" +
                " FROM `news_detail` limit ?,?";
        Object [] param = {(curPage-1)*pageNum,pageNum};
        try {
            rs = select(sql,param);
            while (rs.next()){
                news = new News(rs.getInt(1),rs.getInt(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getString(6),
                        rs.getString(7),rs.getDate(8),rs.getDate(9));
                newsList.add(news);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(ps,rs,conn);
        }
        return newsList;
    }

    public ResultSet select(String sql,Object [] param){
        try {
            ps = conn.prepareStatement(sql);
            if (param!=null){
                for (int i = 1; i <= param.length; i++) {
                    ps.setObject(i,param[i-1]);
                }
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public int update(String sql,Object [] param){
        try {
            ps = conn.prepareStatement(sql);
            if (param!=null){
                for (int i = 1; i <= param.length; i++) {
                    ps.setObject(i,param[i-1]);
                }
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
